/* 
Set directory 
*/

    global root ENTER DIRECTORY

	global r4rawyc 		$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\yc_ch_anon
	global compositer4 	$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\yc_hh_anon
	global r3rawyc 		$root\Data\Young Lives\R3\survey_data_r3\india_r3\youngerchild
	global compositer3 	$root\Data\Young Lives\R3\composite_scores_r3\india_r3
	global rawr2 		$root\Data\Young Lives\R2\survey_data_r2\india_r2\Younger Child
	global compositer2 	$root\Data\Young Lives\R2\composite_scores_r2\india_r2
	global r1 			$root\Data\Young Lives\R1\survey_data_r1\india_r1
	global data 		$root\Dta\Young Lives\

*************************************************
*	MERGING\APPENDING RELEVANT VARIABLES
*************************************************
*Wave 4

use "$r4rawyc\in_r4_ycch_youngerchild.dta", clear
rename *, lower
gen tempin = "IN"
gen tempino = "IN0"
egen childid_1 = concat(tempin childcode)
egen childid_2 = concat(tempino childcode)
gen childid = childid_1
replace childid = childid_2 if childcode<99999
keep childid  zhfa zbfa fhfa fbfa bmi enrschr4 agemon

gen round = 5

save "$data\in_r4_ycch_youngerchild.dta", replace	

use "$compositer4\in_r4_ychh_youngerhousehold.dta", clear
rename *, lower
gen tempin = "IN"
gen tempino = "IN0"
egen childid_1 = concat(tempin childcode)
egen childid_2 = concat(tempino childcode)
gen childid = childid_1
replace childid = childid_2 if childcode<99999
keep childid cd cookingq drwaterq elecq foodexp foodexppc foodexpr foodexprpc hhsize hq nfoodexp nfoodexppc nfoodexpr nfoodexprpc sv toiletq totalexp totalexppc totalexpr totalexprpc wi

gen round = 5

save "$data\in_r4_ychh_youngerhousehold.dta", replace

use "$compositer4\in_r4_ychh_householdrosterr4.dta", clear
rename *, lower

gen tempin = "IN"
gen tempino = "IN0"
egen childid_1 = concat(tempin childcode)
egen childid_2 = concat(tempino childcode)
gen childid = childid_1
replace childid = childid_2 if childcode<99999
rename memidr4 id

keep if relater3 == 0

keep childid perfr4 sleepr4 chcarer4 hhchrer4 npywrkr4 paywrkr4 schoolr4 studyr4 playr4

drop if childid == "IN100015" & perfr4 == .

gen round = 5

save "$data\in_r4_ychh_householdrosterr4.dta", replace

*************************************************
*Wave 3

use "$r3rawyc\in_yc_householdmemberlevel.dta", clear
rename *, lower

drop if childid == "IN100015" & perfr3 == .

keep if relate == 0

keep childid perfr3 sleepr3 chcarer3 hhchrer3 npywrkr3 paywrkr3 schoolr3 studyr3 playr3

gen round = 3

save "$data\in_yc_householdmemberlevel.dta", replace	

use "$compositer3\in_consumption_3yc.dta", clear
rename *, lower

gen round = 3

save "$data\in_consumption_3yc.dta", replace	

use "$r3rawyc\in_yc_childlevel.dta", clear
rename *, lower
keep childid enrschr3 bmi zwfa zhfa zbfa fwfa fhfa fbfa

gen round = 3

save "$data\in_yc_childlevel.dta", replace	

use "$r3rawyc\in_yc_householdlevel.dta", clear
rename *, lower
keep childid clustid commid region typesite sex hhsize hq cd elecq drwaterq toiletq cookingq sv wi ///
totalexp_rpc totalexp_r totalexp_pc totalexp nfoodexp_rpc nfoodexp_r nfoodexp_pc nfoodexp foodexp_rpc foodexp_r foodexp_pc foodexp

gen round = 3

save "$data\in_yc_householdlevel.dta", replace	

*********************************************************
*Wave 2

use "$rawr2\inchildlevel5yrold.dta", clear
rename *, lower
keep childid sex clustid commid typesite region daded mumed bornbef chldeth agechild  ///
bmi zwfa zhfa zbfa fwfa fhfa fbfa zwfl fwfl foodexp nfoodexp totalexp foodexp_pc nfoodexp_pc totalexp_pc foodexp_r nfoodexp_r totalexp_r foodexp_rpc nfoodexp_rpc totalexp_rpc hq cd elecq toiletq drwaterq cookingq sv wi

gen round = 2

save "$data\inchildlevel5yrold.dta", replace	

use "$rawr2\insubhouseholdmember5.dta", clear
rename *, lower

keep if relate == 0

keep childid perf

gen round = 2

save "$data\insubhouseholdmember5.dta", replace	

use "$compositer2\in_consumption_2yc.dta", clear
rename *, lower

gen round = 2

save "$data\in_consumption_2yc.dta", replace
	
********************************************************
*Wave 1

use "$r1\inchildlevel1yrold.dta", clear
rename *, lower
keep childid clustid typesite hhsize commid region sex agechild ageorder clustid chldrel bmi zwfa zhfa zbfa zwfl fwfl fhfa fwfa fbfa hq cd elecq toiletq drwaterq cookingq sv wi

gen round = 1

save "$data\inchildlevel1yrold.dta", replace	

*****************************************************
*Merge Within Waves
*Wave 2
use "$data\inchildlevel5yrold.dta", clear
merge 1:1 childid using "$data\insubhouseholdmember5.dta"
keep if _m == 3
drop _m
merge 1:1 childid using "$data\in_consumption_2yc.dta"
keep if _m == 3
drop _m
save "$data\oth_wave2.dta", replace

*Wave 3
use "$data\in_yc_householdmemberlevel.dta", clear
merge m:1 childid using "$data\in_consumption_3yc.dta"
keep if _m == 3
drop _m
merge 1:1 childid using "$data\in_yc_childlevel.dta"
keep if _m == 3
drop _m
merge 1:1 childid using "$data\in_yc_householdlevel.dta"
keep if _m == 3
drop _m
save "$data\oth_wave3.dta", replace

*Wave 4
use "$data\in_r4_ycch_youngerchild.dta", clear
merge 1:1 childid using "$data\in_r4_ychh_youngerhousehold.dta"
keep if _m == 3
drop _m
merge 1:1 childid using "$data\in_r4_ychh_householdrosterr4.dta"
keep if _m == 3
drop _m
save "$data\oth_wave4.dta", replace

*Append Waves
use "$data\inchildlevel1yrold.dta", clear

append using "$data\oth_wave2.dta"
append using "$data\oth_wave3.dta"
append using "$data\oth_wave4.dta"

gen cohort = 0

save "$data\YC R1234.dta", replace

